#include "text.hpp"
using namespace my_pthread;

int gal = 100;

void function1(ThreadData *data)
{
    while (true)
    {
        //申请锁
        usleep(5000);
        mypthread_mutex_t ret(data->_mutex);
        if (data->_total > 0)
        {        
            data->_total--;
            data->_tick++;
        }
        else
        {
            break;
        }
    }
}

int all = 5;

int main()
{
    // 放线程
    vector<mypthread<ThreadData>> v1;
    // 放pthreadData线程
    vector<ThreadData *> v2;

    // 创建锁并初始化
    pthread_mutex_t mutex_id;
    pthread_mutex_init(&mutex_id, nullptr);

    // 创建一批线程
    for (int i = 1; i <= all; i++)
    {
        string str = "pthread-" + to_string(i);
        ThreadData *tmp = new ThreadData(str, gal, mutex_id);
        v1.emplace_back(str, function1, tmp);
        v2.emplace_back(tmp);
    }

    // 启动一批线程
    for (auto &e : v1)
    {
        e.start();
    }

    // 等待一批线程
    for (auto &e : v1)
    {
        e.join();
    }

    for(auto e : v2)
    {
        cout << e->_name << " :" << e->_tick << endl;
    }

    return 0;
}